﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using classMeeting.App_Code;

namespace classMeeting.Admin
{
    public partial class Declaration : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            checkLogin();
            if (!IsPostBack)
                bindTable();
        }

        protected void sub_Click(object sender, EventArgs e)
        {
            checkLogin();
            subData();
        }

        public String getId()
        {
            string ID = "";
            try
            {
                ID = System.Text.RegularExpressions.Regex.Replace(System.Web.HttpContext.Current.Server.HtmlDecode(Request.QueryString["UrlID"].ToString()), "'", "");
            }
            catch (NullReferenceException e)
            {
                Response.Redirect("Home.aspx");
            }
            return ID;
        }

        protected void bindTable()
        {
            String name = (String)Session["name"];
            //name = "测试学校管理员";
            String ID = "";
            try
            {
                ID = System.Text.RegularExpressions.Regex.Replace(System.Web.HttpContext.Current.Server.HtmlDecode(Request.QueryString["UrlID"].ToString()), "'", "");
            }
            catch (NullReferenceException e)
            {
                Response.Redirect("Home.aspx");
            }
            finally
            {
                DataSet dt = SqlHelper.ExecuteDataset(CommandType.Text, "SELECT * FROM [classMeeting].[dbo].[project] WHERE [id] = '" + ID + "'");
                if (dt.Tables[0].Rows.Count > 0)
                {
                    labOrganization.Text = dt.Tables[0].Rows[0]["organization"].ToString();
                    labMeetingName.Text = dt.Tables[0].Rows[0]["meetingName"].ToString();
                    labTime.Text = dt.Tables[0].Rows[0]["time"].ToString();
                    labAddress.Text = dt.Tables[0].Rows[0]["address"].ToString();
                    labLeader.Text = dt.Tables[0].Rows[0]["leader"].ToString();
                    labLeaderPhone.Text = dt.Tables[0].Rows[0]["leaderPhone"].ToString();
                    labTarget.Text = dt.Tables[0].Rows[0]["target"].ToString();
                    labParticipants.Text = dt.Tables[0].Rows[0]["participants"].ToString();
                    labAdviser.Text = dt.Tables[0].Rows[0]["adviser"].ToString();
                    labAdviserPosition.Text = dt.Tables[0].Rows[0]["adviserPosition"].ToString();
                    labAdviserPhone.Text = dt.Tables[0].Rows[0]["adviserPhone"].ToString();
                    labGoal.Text = dt.Tables[0].Rows[0]["goal"].ToString();
                    labDetails.Text = dt.Tables[0].Rows[0]["details"].ToString();
                    labPlan.Text = dt.Tables[0].Rows[0]["plan"].ToString();
                    labSafe.Text = dt.Tables[0].Rows[0]["safe"].ToString();
                    labBudget.Text = dt.Tables[0].Rows[0]["budget"].ToString();
                    labCharge.Text = dt.Tables[0].Rows[0]["charge"].ToString();
                    labMonitor.Text = dt.Tables[0].Rows[0]["monitor"].ToString() + "<br />&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp" + dt.Tables[0].Rows[0]["subTime"].ToString();
                    labClaInsSug.Text = dt.Tables[0].Rows[0]["claInsSug"].ToString() + "<br />&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp" + dt.Tables[0].Rows[0]["claInsTime"].ToString();
                    labColInsSug.Text = dt.Tables[0].Rows[0]["colInsSug"].ToString() + "<br />&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp" + dt.Tables[0].Rows[0]["colInsTime"].ToString();
                    txtAdmSug.Text = dt.Tables[0].Rows[0]["admSug"].ToString();
                    String sqlTheme = dt.Tables[0].Rows[0]["theme"].ToString();
                    String isView = dt.Tables[0].Rows[0]["isView"].ToString();
                    if (sqlTheme.Equals("1"))
                        theme1.Checked = true;
                    else if (sqlTheme.Equals("2"))
                        theme2.Checked = true;
                    else if (sqlTheme.Equals("3"))
                        theme3.Checked = true;
                    else
                    {
                        theme4.Checked = true;
                        labOTheme.Text = sqlTheme;
                    }
                    if (isView.Equals("是"))
                        view1.Checked = true;
                    else if (isView.Equals("否"))
                        view2.Checked = false;
                }
                else {
                    Response.Redirect("Home.aspx");
                }
            }

        }

        protected void checkLogin()
        {
            String name = (String)Session["name"];
            //name = "测试学校管理员";
            String schoolId = (String)Session["schoolId"];
            CheckLogin check = new CheckLogin(name, schoolId);
            DataSet dt = SqlHelper.ExecuteDataset(CommandType.Text, "SELECT * FROM [classMeeting].[dbo].[project] WHERE [id] = '" + getId() + "'");
            if ((!check.isAdmin())||dt.Tables[0].Rows.Count==0)
            {
                Response.Redirect("http://www.yiban.cn/school/index/id/5000207");
            }
            DataSet dt1 = SqlHelper.ExecuteDataset(CommandType.Text, "SELECT * FROM [classMeeting].[dbo].[admin] WHERE [schoolId] = '" + schoolId + "'");
            labUser.Text = (string)dt1.Tables[0].Rows[0]["name"];
        }

        protected void subData()
        {
            String subTime = DateTime.Now.ToLongDateString();
            String admSug = txtAdmSug.Text;
            String theme = "";
            String name = (String)Session["name"];
            //name = "测试学校管理员";
            String schoolId = (String)Session["schoolId"];
            if (theme1.Checked)
                theme = "1";
            else if (theme2.Checked)
                theme = "2";
            else if (theme3.Checked)
                theme = "3";
            else if (theme4.Checked)
                theme = labOTheme.Text;
            if (admSug.Equals(""))
            {
                Response.Write("<Script>alert('请将所有项目填写完毕！');</Script>");
                return;
            }
            int judNum = 0;
            judNum = SqlHelper.ExecuteNonQuery(CommandType.Text, "UPDATE [classMeeting].[dbo].[project] SET [state] = '已通过审核',[admSug] = '" + admSug + "',[admTime] = '" + subTime  + "'WHERE [id]='" + getId() + "' AND [hisAdmin] ='"+name+"' ");
            if (judNum > 0)
            {
                Response.Write("<Script>alert('提交成功！');window.location.href='Home.aspx';</Script>");
                return;
            }
            else
            {
                Response.Write("<Script>alert('提交失败，请重试或联系网站负责人！');</Script>");
                return;
            }
        }

        protected void refData()
        {
            String subTime = DateTime.Now.ToLongDateString();
            String admSug = txtAdmSug.Text;
            String theme = "";
            String name = (String)Session["name"];
            //name = "测试学校管理员";
            String schoolId = (String)Session["schoolId"];
            if (theme1.Checked)
                theme = "1";
            else if (theme2.Checked)
                theme = "2";
            else if (theme3.Checked)
                theme = "3";
            else if (theme4.Checked)
                theme = labOTheme.Text;
            if (admSug.Equals(""))
            {
                Response.Write("<Script>alert('请将所有项目填写完毕！');</Script>");
                return;
            }
            int judNum = 0;
            judNum = SqlHelper.ExecuteNonQuery(CommandType.Text, "UPDATE [classMeeting].[dbo].[project] SET [state] = '被学校管理员驳回',[admSug] = '" + admSug + "',[admTime] = '" + subTime + "'WHERE [id]='" + getId() + "' AND [hisAdmin] ='" + name + "' ");
            if (judNum > 0)
            {
                Response.Write("<Script>alert('提交成功！');window.location.href='Home.aspx';</Script>");
                return;
            }
            else
            {
                Response.Write("<Script>alert('提交失败，请重试或联系网站负责人！');</Script>");
                return;
            }
        }

        protected void ref_Click(object sender, EventArgs e)
        {
            checkLogin();
            refData();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            String subTime = DateTime.Now.ToLongDateString();
            String admSug = txtAdmSug.Text;
            String theme = "";
            String name = (String)Session["name"];
            //name = "测试学校管理员";
            String schoolId = (String)Session["schoolId"];
            if (theme1.Checked)
                theme = "1";
            else if (theme2.Checked)
                theme = "2";
            else if (theme3.Checked)
                theme = "3";
            else if (theme4.Checked)
                theme = labOTheme.Text;
            int judNum = 0;
            judNum = SqlHelper.ExecuteNonQuery(CommandType.Text, "UPDATE [classMeeting].[dbo].[project] SET [state] = '等待学校管理员审核',[admSug] = '" + admSug + "',[admTime] = '" + subTime + "'WHERE [id]='" + getId() + "' AND [hisAdmin] ='" + name + "' ");
            if (judNum > 0)
            {
                Response.Write("<Script>alert('保存成功！');window.location.href='Home.aspx';</Script>");
                return;
            }
            else
            {
                Response.Write("<Script>alert('保存失败，请重试或联系网站负责人！');</Script>");
                return;
            }
        }

        protected void libExit_Click(object sender, EventArgs e)
        {
            Session["name"] = null;
            Session["schoolId"] = null;
            Session["college"] = null;
            Response.Redirect("http://www.yiban.cn/school/index/id/5000207");

        }
    }
}